﻿package com.cf.papervision3d.model{
	import org.papervision3d.typography.*;
	import org.papervision3d.typography.fonts.*;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.materials.special.LineMaterial;
	import org.papervision3d.materials.special.Letter3DMaterial;
	import org.papervision3d.core.geom.Lines3D;
	import org.papervision3d.core.geom.renderables.Line3D;
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.core.math.Number3D;
	
	public class axis extends DisplayObject3D{
		private var _fx:Text3D;
		private var _fy:Text3D;
		private var _fz:Text3D;
		private var _fontsize:Number;
		private var _currentSize:Number;
		private var _fontSpace:Number;
		private var _fontFix:Number;
		
		public function axis(_size:Number=100):void{
			_currentSize = _size;
			_fontSpace = _currentSize * 0.3;
			_fontsize = _currentSize/100;
			_fontFix = _currentSize/10;
			// x-, y- and z-axis
			// Create a default line material and a Lines3D object (container for Line3D objects)
			var defaultMaterial:LineMaterial = new LineMaterial(0xFFFFFF);
			var _axes:Lines3D = new Lines3D(defaultMaterial);
			// Create a different colour line material for each axis
			var xAxisMaterial:LineMaterial = new LineMaterial(0xFF0000); // red
			var yAxisMaterial:LineMaterial = new LineMaterial(0x00FF00); // green
			var zAxisMaterial:LineMaterial = new LineMaterial(0x0000FF); // blue
			// Create a origin vertex
			var origin:Vertex3D = new Vertex3D(0, 0, 0);
			// Create a new line (length 100) for each axis using the different materials and a width of 2.
			var xAxis:Line3D = new Line3D(_axes, xAxisMaterial, 2, origin, new Vertex3D(_currentSize, 0, 0));
			var yAxis:Line3D = new Line3D(_axes, yAxisMaterial, 2, origin, new Vertex3D(0, _currentSize, 0));
			var zAxis:Line3D = new Line3D(_axes, zAxisMaterial, 2, origin, new Vertex3D(0, 0, -_currentSize)); 
			// Add lines to the Lines3D container
			_axes.addLine(xAxis);
			_axes.addLine(yAxis);
			_axes.addLine(zAxis);
			
			var _fontfamily:Font3D = new HelveticaLight();
			var _fontMatrix:Letter3DMaterial;
			_fontMatrix = new Letter3DMaterial(0xFF0000);
			_fontMatrix.doubleSided = true;
			
			_fx = new Text3D("x", _fontfamily, _fontMatrix);
			_fx.x = _currentSize + _fontSpace;
			_fx.y = _fontFix;
			_fx.z = 0;
			_fx.scale = _fontsize;
			this.addChild(_fx);
			
			_fontMatrix = new Letter3DMaterial(0x00FF00);
			_fontMatrix.doubleSided = true;
			_fy = new Text3D("y", _fontfamily, _fontMatrix);
			_fy.x = 0;
			_fy.y = _currentSize + _fontSpace + _fontFix;
			_fy.z = 0;
			_fy.scale = _fontsize;
			this.addChild(_fy);
			
			_fontMatrix = new Letter3DMaterial(0x0000FF);
			_fontMatrix.doubleSided = true;
			_fz = new Text3D("z", _fontfamily, _fontMatrix);
			_fz.x = 0;
			_fz.y = _fontFix;
			_fz.z = -(_currentSize + _fontSpace);
			_fz.scale = _fontsize;
			this.addChild(_fz);
			
			this.addChild(_axes);
			
		}//fn
		
		// make the text [x,y,z] lookAt the camera or something else
		public function update(_obj:DisplayObject3D=null):void{
			if(_obj!=null){
				_fx.lookAt(_obj); _fx.yaw(180);
				_fy.lookAt(_obj); _fy.yaw(180);
				_fz.lookAt(_obj); _fz.yaw(180);
			}//if
		}//fn
		

	}//class
}//package